package com.songlanyun.modules.servicegoods.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.GoodsExceptionEnum;
import com.songlanyun.modules.exception.ServiceGoodsExceptionEnum;
import com.songlanyun.modules.servicegoods.model.dto.ServiceGoodsClassifyDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.servicegoods.dao.ServiceGoodsClassifyDao;
import com.songlanyun.modules.servicegoods.entity.ServiceGoodsClassify;
import com.songlanyun.modules.servicegoods.service.ServiceGoodsClassifyService;


@Service("serviceGoodsClassifyService")
public class ServiceGoodsClassifyServiceImpl extends ServiceImpl<ServiceGoodsClassifyDao, ServiceGoodsClassify> implements ServiceGoodsClassifyService {

    @Override
    public ServiceGoodsClassify create(ServiceGoodsClassify entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public ServiceGoodsClassify modify(ServiceGoodsClassify entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {
        this.removeById(id);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = MapUtil.getStr(params, "name");
        IPage<ServiceGoodsClassify> page = this.page(
                new Query<ServiceGoodsClassify>().getPage(params),
                new LambdaQueryWrapper<ServiceGoodsClassify>()
                        .like(StringUtils.isNotBlank(name), ServiceGoodsClassify::getName, name)
                        .orderByDesc(ServiceGoodsClassify::getCreateTime)
        );

        return new PageUtils(page);
    }

    /**
     * 按id查询商品分类
     * @param id
     * @param isThrow
     * @return
     */
    @Override
    public ServiceGoodsClassify getById(Long id, boolean isThrow) {
        ServiceGoodsClassify classify = this.baseMapper.selectById(id);
        if (classify == null) {
            if (isThrow) {
                throw new RRException(ServiceGoodsExceptionEnum.SERVICE_CATEGORY_NOT_EXISTS);
            }
        }
        return classify;
    }

    /**
     * 添加平台的服务分类
     * @param dto
     * @return
     */
    @Override
    public ServiceGoodsClassify add(ServiceGoodsClassifyDTO dto) {
        ServiceGoodsClassify classify = new ServiceGoodsClassify();
        BeanUtil.copyPropertiesIgnoreNull(dto, classify);
        validateNameRepeat(classify);

        ServiceGoodsClassify serviceGoodsClassify = this.create(classify);
        return serviceGoodsClassify;
    }

    /**
     * 修改平台的服务分类
     * @param dto
     * @return
     */
    @Override
    public ServiceGoodsClassify update(ServiceGoodsClassifyDTO dto) {
        ServiceGoodsClassify obj = this.getById(dto.getId(),true);
        validateNameRepeat(obj);
        BeanUtil.copyPropertiesIgnoreNull(dto, obj);
        this.modify(obj);
        return obj;
    }

    /**
     * 查询所有平台的服务分类
     * @return
     */
    @Override
    public List<ServiceGoodsClassify> getAllCategory() {
        return this.list(new LambdaQueryWrapper<ServiceGoodsClassify>()
                .orderByAsc(ServiceGoodsClassify::getOrderIndex)
                .orderByDesc(ServiceGoodsClassify::getCreateTime));
    }

    private void validateNameRepeat(ServiceGoodsClassify classify) {
        ServiceGoodsClassify target = this.getOne(new LambdaQueryWrapper<ServiceGoodsClassify>()
                .eq(ServiceGoodsClassify::getName, classify.getName())
                .last("limit 1"));
        if (target != null && !target.equals(classify)) {
            throw new RRException(ServiceGoodsExceptionEnum.SERVICE_CATEGORY_NAME_REPEAT);
        }
    }

}